package com.lanmon.sqlite;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.lanmon.dao.ExamDao;
import com.lanmon.pojo.Exam;

public class InitDabaBase {
	public static void main(String[] args) throws Exception {
		try (Connection connection = SqliteConnection.getConnection()){
			List<Exam> a = new ExamDao().queryAll();
			System.out.println(a);
		}
		catch (Exception e) {
			
		}finally{
			//SqliteConnection.close(connection);
		}
		//System.out.println(conn);
		
	}
	
	public static void initDabaBase() {
		Connection connection = null;
		try {
			connection = SqliteConnection.getConnection();
			connection.setAutoCommit(false);// 控制非自动提交事务。也就是开启了事务
			Statement statement = connection.createStatement();
			statement.setQueryTimeout(30); // set timeout to 30 sec.
			statement.executeUpdate("drop table if exists person");// ;
			statement.executeUpdate("create table exam (id integer, title string, body string, answer string, primary key(id));");// ;
			connection.commit();// 手动提交事务
			System.out.println("==================数据库初始化成功！==================");
		}
		catch (SQLException e) {
			System.err.println(e.getMessage());
		}
		finally {
			try {
				if (connection != null)
					connection.close();
			}
			catch (SQLException e) {
				System.err.println(e);
			}
		}
	}
}
